﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web
{
    public partial class ExpScoreEdit : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSCJLR");
            strPageUserType.Add("teacher");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                YearDDL_DataBind();
                ExScoreGridView_DataBind();
            }
        }

        //绑定Gridview
        protected void ExScoreGridView_DataBind()
        {
            List<ExtendModule.CourseAndClassItem> list = new List<ExtendModule.CourseAndClassItem>();
            List<LabMS.Model.Course> courList = new List<LabMS.Model.Course>();

            courList = GetCourseInfo(TermDDL.SelectedValue, YearDDL.SelectedValue, SubjectTBX.Text.Trim());
            list = HandList(courList);

            ExScoreGridView.DataSource = list;
            ExScoreGridView.DataBind();
        }

        protected void ExGV_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

                e.Row.Cells[5].Attributes.Add("OnDblClick", "window.location.href='./AddScore.aspx?ID=" + ExScoreGridView.DataKeys[ExScoreGridView.DataKeys.Count - 1].Value.ToString() + "'");
                e.Row.Cells[5].Attributes["style"] = "Cursor:pointer";
            }

        }

        //获取获取课程信息
        protected List<LabMS.Model.Course> GetCourseInfo(string ScoreTerm, string Year, string SubjectName)
        {
            LabMS.BLL.Course course = new LabMS.BLL.Course();
            List<LabMS.Model.Course> courseModel = new List<LabMS.Model.Course>();
            try
            {
                courseModel = course.GetModelList("[ID] in (select a.CourseID from CourseAndProject a,TeacherCourse b where a.CourseID=b.CourseID) and CourseYear='" + Year + "' and CourseSeason='" + ScoreTerm + "' and Course_Name like '%" + SubjectName + "%'");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return courseModel;
        }

        //通过课程ID获取选课班级信息
        protected List<LabMS.Model.ClassItem> GetClassItemInfo(int CourseID)
        {
            LabMS.BLL.ClassItem classitem = new LabMS.BLL.ClassItem();
            List<LabMS.Model.ClassItem> classitenList = new List<LabMS.Model.ClassItem>();
            try
            {
                classitenList = classitem.GetModelList("CourseID=" + CourseID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return classitenList;
        }
        //处理CourseList
        protected List<ExtendModule.CourseAndClassItem> HandList(List<LabMS.Model.Course> courList)
        {
            List<ExtendModule.CourseAndClassItem> list = new List<ExtendModule.CourseAndClassItem>();
            List<LabMS.Model.ClassItem> classItemList = new List<LabMS.Model.ClassItem>();
            for (int i = 0; i < courList.Count; i++)
            {
                ExtendModule.CourseAndClassItem courClassItem = new ExtendModule.CourseAndClassItem();
                classItemList = GetClassItemInfo(courList[i].ID);

                courClassItem.ID = courList[i].ID;
                courClassItem.Course_Name = courList[i].Course_Name;
                courClassItem.Course_Code = courList[i].Course_Code;
                courClassItem.Course_ExprientHouse = courList[i].Course_ExprientHouse;
                for (int j = 0; j < classItemList.Count; j++)
                {
                    courClassItem.ClassItem_Name += classItemList[j].ClassItem_Name;
                    if (courClassItem.ClassItem_Num == null)
                    {
                        courClassItem.ClassItem_Num = 0;
                    }
                    courClassItem.ClassItem_Num += classItemList[j].ClassItem_Num;

                    courClassItem.ClassItem_Name += ((j % 5 == 4) ? "<br />" : "&nbsp;");
                }
                if (classItemList.Count > 0 && classItemList.Count % 5 == 0)
                {
                    string str = courClassItem.ClassItem_Name;
                    courClassItem.ClassItem_Name = str.Substring(0, str.Length - 6);
                }

                list.Add(courClassItem);
            }

            return list;
        }

        //缩影改变 ddl
        protected void DropDownList_OnSelectedIndexChange(object sender, EventArgs e)
        {
            ExScoreGridView_DataBind();
        }
        //绑定学年
        protected void YearDDL_DataBind()
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetModelList("DClass = 'ruxuenianfen' order by Code desc");
            YearDDL.DataSource = listCClass;
            YearDDL.DataTextField = "Name";
            YearDDL.DataValueField = "Code";
            YearDDL.DataBind();
        }

        //分页
        protected void ExScoreGV_OnPageIndexChange(object sender, GridViewPageEventArgs e)
        {
            ExScoreGridView.PageIndex = e.NewPageIndex;
            ExScoreGridView_DataBind();
        }

        protected void Search_OnClick(object sender, EventArgs e)
        {
            ExScoreGridView_DataBind();
        }
    }
}
